<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="区块链类型" prop="dagType">
        <el-select v-model="queryParams.dagType" placeholder="请选择区块链类型" clearable>
          <el-option
            v-for="dict in dict.type.dag_blockchain_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工作方式" prop="work">
        <el-select v-model="queryParams.work" placeholder="请选择工作方式" clearable>
          <el-option
            v-for="dict in dict.type.dag_perform_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="持续时间" prop="duration">
        <el-input
          v-model="queryParams.duration"
          placeholder="请输入持续时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送速率" prop="txRate">
        <el-input
          v-model="queryParams.txRate"
          placeholder="请输入发送速率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送数量" prop="senderNum">
        <el-input
          v-model="queryParams.senderNum"
          placeholder="请输入发送数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="查询ip" prop="queryIp">
        <el-input
          v-model="queryParams.queryIp"
          placeholder="请输入查询ip"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="查询port" prop="queryPort">
        <el-input
          v-model="queryParams.queryPort"
          placeholder="请输入查询port"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区块高度" prop="blockLength">
        <el-input
          v-model="queryParams.blockLength"
          placeholder="请输入区块高度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:configNet:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:configNet:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:configNet:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:configNet:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configNetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="接收者">
              <span>{{ scope.row.resvField1 }}</span>
            </el-form-item>
            <el-form-item label="协调器间隔">
              <span>{{ scope.row.resvField2 }}</span>
            </el-form-item>
            <el-form-item label="节点列表">
              <span>{{ scope.row.nodeUrl }}</span>
            </el-form-item>
            <el-form-item label="payload">
              <span>{{ scope.row.resvField3 }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="唯一标识符" align="center" prop="uuid">
        <template slot-scope="scope">
          <span>{{ scope.row.uuid.substring(0, 9)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="区块链类型" align="center" prop="dagType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dag_blockchain_type" :value="scope.row.dagType"/>
        </template>
      </el-table-column>
      <el-table-column label="工作方式" align="center" prop="work">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dag_perform_type" :value="scope.row.work"/>
        </template>
      </el-table-column>
      <el-table-column label="持续时间" align="center" prop="duration" />
      <el-table-column label="发送速率" align="center" prop="txRate" />
      <el-table-column label="发送数量" align="center" prop="senderNum" />
<!--      <el-table-column label="节点列表" align="center" prop="nodeUrl" />-->
      <el-table-column label="查询ip" align="center" prop="queryIp" />
      <el-table-column label="查询port" align="center" prop="queryPort" />
<!--      <el-table-column label="备用1" align="center" prop="resvField1" />-->
<!--      <el-table-column label="备用2" align="center" prop="resvField2" />-->
<!--      <el-table-column label="备用3" align="center" prop="resvField3" />-->
<!--      <el-table-column label="备用4" align="center" prop="resvField4" />-->
      <el-table-column label="区块高度" align="center" prop="blockLength" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:configNet:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:configNet:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改网络配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="区块链类型" prop="dagType">
          <el-select v-model="form.dagType" placeholder="请选择区块链类型">
            <el-option
              v-for="dict in dict.type.dag_blockchain_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作方式">
          <el-radio-group v-model="form.work">
            <el-radio
              v-for="dict in dict.type.dag_perform_type"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="持续时间" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入持续时间" />
        </el-form-item>
        <el-form-item label="发送速率" prop="txRate">
          <el-input v-model="form.txRate" placeholder="请输入发送速率" />
        </el-form-item>
        <el-form-item label="发送数量" prop="senderNum">
          <el-input v-model="form.senderNum" placeholder="请输入发送数量" />
        </el-form-item>
        <el-form-item label="节点列表" prop="nodeUrl">
          <el-input v-model="form.nodeUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="查询ip" prop="queryIp">
          <el-input v-model="form.queryIp" placeholder="请输入查询ip" />
        </el-form-item>
        <el-form-item label="查询port" prop="queryPort">
          <el-input v-model="form.queryPort" placeholder="请输入查询port" />
        </el-form-item>

<!--        当dag为iota时-->
        <el-form-item v-if="form.dagType == 0" label="接收者" prop="resvField1">
          <el-input v-model="form.resvField1" placeholder="请输入备用1" />
        </el-form-item>
        <el-form-item v-if="form.dagType == 0 && form.work == 0" label="协调器间隔" prop="resvField2">
          <el-input v-model="form.resvField2" type="textarea" placeholder="请输入交易数据" />
        </el-form-item>
<!--        当dag为iota，且工作方式为valuetransfer时-->
        <el-form-item v-if="form.dagType == 0 && form.work == 0" label="交易数据" prop="resvField3">
          <el-input v-model="form.resvField3" type="textarea" placeholder="请输入交易数据" />
        </el-form-item>

        <el-form-item v-if="form.dagType == 1" label="nano-key" prop="resvField1">
          <el-input v-model="form.resvField1" type="textarea" placeholder="请输入交易数据" />
        </el-form-item>


<!--        <el-form-item label="备用1" prop="resvField1">-->
<!--          <el-input v-model="form.resvField1" placeholder="请输入备用1" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用2" prop="resvField2">-->
<!--          <el-input v-model="form.resvField2" placeholder="请输入备用2" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用3" prop="resvField3">-->
<!--          <el-input v-model="form.resvField3" placeholder="请输入备用3" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用4" prop="resvField4">-->
<!--          <el-input v-model="form.resvField4" placeholder="请输入备用4" />-->
<!--        </el-form-item>-->
        <el-form-item label="区块高度" prop="blockLength">
          <el-input v-model="form.blockLength" placeholder="请输入区块高度" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfigNet, getConfigNet, delConfigNet, addConfigNet, updateConfigNet } from "@/api/system/configNet";

export default {
  name: "ConfigNet",
  dicts: ['dag_perform_type', 'dag_blockchain_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 网络配置表格数据
      configNetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dagType: null,
        work: null,
        duration: null,
        txRate: null,
        senderNum: null,
        nodeUrl: null,
        queryIp: null,
        queryPort: null,
        resvField1: null,
        resvField2: null,
        resvField3: null,
        resvField4: null,
        blockLength: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询网络配置列表 */
    getList() {
      this.loading = true;
      listConfigNet(this.queryParams).then(response => {
        this.configNetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        uuid: null,
        dagType: null,
        work: 0,
        duration: null,
        txRate: null,
        senderNum: null,
        nodeUrl: null,
        queryIp: null,
        queryPort: null,
        resvField1: null,
        resvField2: null,
        resvField3: null,
        resvField4: null,
        blockLength: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.uuid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加网络配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const uuid = row.uuid || this.ids
      getConfigNet(uuid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改网络配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.uuid != null) {
            updateConfigNet(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfigNet(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const uuids = row.uuid || this.ids;
      this.$modal.confirm('是否确认删除网络配置编号为"' + uuids + '"的数据项？').then(function() {
        return delConfigNet(uuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/configNet/export', {
        ...this.queryParams
      }, `configNet_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
